<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>供应商信息</span>
      </div>
      <div class="text item">
        <el-table
          ref="monthlyPlanTable"
          :data="sourcingSupplierList"
          border
          stripe
          :header-cell-style="{ textAlign: 'center' }"
          :cell-style="{ textAlign: 'center' }"
          highlight-current-row
          @current-change="handleCurrentChange"
        >
          <el-table-column label="供应商名称" prop="supplierName" />
          <el-table-column label="供应商编码" prop="supplierCode" />
          <el-table-column label="供应商状态" prop="supplierStatusStr" />
          <el-table-column label="是否生成订单" prop="isCreateOrder">
            <template slot-scope="scope">
              <div>{{ scope.row.isCreateOrder | isCreateOrderfn }}</div>
            </template>
          </el-table-column>
          <el-table-column label="供应商联系人" prop="supplierLinkman" />
          <el-table-column label="供应商电话" prop="supplierPhone" />
        </el-table>
      </div>
    </el-card>
    <el-card class="box-card margin-top">
      <div slot="header" class="clearfix">
        <span>已中标商品明细</span>
      </div>
      <div class="text item">
        <basic-list
          :tableHeight="400"
          :show-select-col="false"
          :data-source="supplierList"
          :columns="columns"
          :show-pagination="false"
          :total="total"
        />
        <div
          slot="footer"
          class="margin-top operate-area el-row is-justify-center el-row--flex"
        >
          <el-button type="primary" @click="pushdownfn" :loading="submitLoading">生成订单</el-button>
          <el-button v-if="!isSelfPurchase" type="primary" @click="exportfn">导出合同</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex';
import pushDownPage from '../pushDownPage/index';
import BasicList from '../../../components/BasicList';
import * as Utils from '../../utils';
import { generateOrderService } from '@api/sourcing/selfPurchaseInquiry'

export default {
  name: 'Index',
  components: {
    pushDownPage,
    BasicList
  },
  data () {
    return {
      submitLoading: false,
      columns: Utils.SET_SOURCINGSUPP_LIST
    };
  },
  computed: {
    ...mapState({
      sourcingSupplierList: (state) =>
        state.sourcing.purchaseInquiry.sourcingSupplierList,
      supplierList: (state) => state.sourcing.purchaseInquiry.supplierList,
      supplierId: (state) => state.sourcing.purchaseInquiry.supplierId,
      sourcingSupplierId: (state) =>
        state.sourcing.purchaseInquiry.sourcingSupplierId,
      isCreateOrdernum: (state) =>
        state.sourcing.purchaseInquiry.isCreateOrdernum,
      total: (state) => state.sourcing.purchaseInquiry.total
    }),
    sourcingBillId () {
      return this.$route.query.sourcingBillId
    },
    pushType () {
      return this.$route.query.pushType
    },
    // 自进货询价下推
    isSelfPurchase () {
      const { pushType } = this.$route.query
      return pushType && pushType == 'selfPurchase'
    }
  },
  created () {
    this.initquerySourcingSupplierList();
  },
  methods: {
    ...mapActions('sourcing/purchaseInquiry', [
      'querySourcingSupplierList', // 供应商信息查询
      'checkSecondPriceList', // 判断单据下商品是否为待二次询价，是否是线索供应商
    ]),
    ...mapMutations('sourcing/purchaseInquiry', ['SET_SUPPLIER_LIST']),
    handleCurrentChange (row) {
      if (row) {
        this.SET_SUPPLIER_LIST(row);
      }
    },
    goBack () {
      this.$router.go(-1);
    },
    pushdownfn () {
      if (this.isCreateOrdernum == 1) {
        return this.$message.warning('此供应商已生成订单，请选择其他供应商');
      }
      const cb = (res) => {
        this.submitLoading = false;
        if (Utils.isResponseSuccess(res)) {
          this.$router.push({
            name: 'selfPurchaseGenerateOrder',
            query: {
              supplierId: this.supplierId,
              sourcingSupplierId: this.sourcingSupplierId,
              pushType: this.pushType || ''
            }
          });
        }
      }
      this.submitLoading = true;
      if (this.isSelfPurchase) {
        generateOrderService([this.sourcingSupplierId]).then(res => {
          cb(res)
        })
      } else {
        this.checkSecondPriceList({
          payload: {
            data: [this.sourcingSupplierId],
            cb
          }
        })
      }
    },
    initquerySourcingSupplierList () {
      const cb = (res) => {};
      const data = {
        sourcingBillId: this.sourcingBillId,
        type: this.isSelfPurchase ? 1 : 0
      }; // 订货单主键id
      this.querySourcingSupplierList({
        payload: {
          data,
          cb
        }
      });
    },
    exportfn () {
      window.location.href = `${window.location.origin}/srm-api/orderInquiry/exportOrderContract?sourcingSupplierId=${this.sourcingSupplierId}`;
    }
  },
  filters: {
    isCreateOrderfn: (val) => {
      switch (val) {
        case 0:
          return '否';
        case 1:
          return '是';
        default:
          return '';
      }
    }
  }
};
</script>
<style scoped>
.margin-top {
  margin-top: 10px;
}
</style>
